package com.ruoyi.cmdb.controller;

import com.ruoyi.cmdb.util.ExecShell;
import com.ruoyi.cmdb.util.GetId;
import com.ruoyi.cmdb.util.Ip;
import com.ruoyi.cmdb.vo.ExecShellRes;
import com.ruoyi.cmdb.vo.IpNetwork;
import com.ruoyi.cmdb.vo.ScanIpAutoReq;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import oshi.driver.windows.wmi.OhmHardware;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * testScanIp
 *
 */
@RestController
@RequestMapping("/cmdb/testScanIp")
@Api(tags = "6.testScanIp")
public class TestScanIpController {
    /**
     * scanAuto
     */
    @SneakyThrows
    @PostMapping("/scanAuto")
    @ApiOperation("2.scanAuto")
    public AjaxResult scanAuto(@RequestBody ScanIpAutoReq scanIpAutoReq)  {
        Ip ip=new Ip();
        //获取本机公网ip
        String internalIp=ip.findClosestInternalIp();
        System.out.println("internalIp:"+internalIp);

        //获取本机内网ip
        List<String> localIpList=ip.findLocalIp();
        String localIps=String.join(",",localIpList);
        System.out.println("localIpList:"+localIps);

        //根据任意一个ip和掩码得出全部广播地址,网络段
        IpNetwork ipNetwork=ip.getNetworkRange("192.168.1.2","255.255.255.192");
        System.out.println("networkId:"+ipNetwork.getNetwork()+",mask:"+ipNetwork.getMask()+",broadcast:"+ipNetwork.getBroadcast());

        //根据给的开始ip和结束ip,得到区间ip段
        List<String> ipsArray=ip.getIpArray(ipNetwork.getNetwork(),ipNetwork.getBroadcast());
        String ipsArrayString=String.join(",",ipsArray);
        System.out.println("ipsArrayString:"+ipsArrayString);

        //检查IP地址是否在指定的范围内
        Boolean b=ip.isBetween("192.168.1.254","192.168.1.1","192.168.3.30");
        System.out.println("192.168.1.254可能在192.168.1.1和192.168.3.30之间:"+b);

        //检测ip状态
        Boolean ipStatus=ip.ipStatus("192.168.32.144");
        System.out.println("192.168.32.144 status:"+ipStatus);

        //获取到达ip的时间
        System.out.println("ping 8.8.8.8 result:"+ip.pingIp("8.8.8.8"));

        //获取本地路由器的外网ip
        String internetIp=ip.getInternetIp();
        System.out.println("本地路由器的外网ip:"+internetIp);

        String rps="ip success sout";
        System.out.println(rps);
        return AjaxResult.success(rps);
    }
}
